package my1024.utils;

//demo for visual object
// not used

import javax.media.j3d.Appearance;
import javax.media.j3d.ColoringAttributes;
import javax.media.j3d.Geometry;
import javax.media.j3d.PolygonAttributes;
import javax.media.j3d.TriangleStripArray;
import javax.vecmath.Point3f;

import my1024.model.VisualObject;

public class BoxObj extends VisualObject {
	@Override
	public Geometry createGeometry() {
		// Array to hold the vertices that make up the side of the box
		Point3f[] sideCoords = new Point3f[10];
		// Vertices for the bottom
		Point3f[] bottomCoords = new Point3f[4];

		// We will use these coordinates for the corners; the box is centered at
		// the origin
		float pos = 0.05f;
		float neg = -0.05f;

		// This goes around the box, defining vertices.
		// Notice that the first 2 vertices are repeated at the end;
		// this is so the strip array goes entirely around the box
		sideCoords[0] = new Point3f(neg, neg, pos);
		sideCoords[1] = new Point3f(neg, pos, pos);
		sideCoords[2] = new Point3f(pos, neg, pos);
		sideCoords[3] = new Point3f(pos, pos, pos);
		sideCoords[4] = new Point3f(pos, neg, neg);
		sideCoords[5] = new Point3f(pos, pos, neg);
		sideCoords[6] = new Point3f(neg, neg, neg);
		sideCoords[7] = new Point3f(neg, pos, neg);
		sideCoords[8] = new Point3f(neg, neg, pos);
		sideCoords[9] = new Point3f(neg, pos, pos);

		// The bottom of the box
		bottomCoords[0] = new Point3f(neg, neg, pos);
		bottomCoords[1] = new Point3f(pos, neg, pos);
		bottomCoords[2] = new Point3f(neg, neg, neg);
		bottomCoords[3] = new Point3f(pos, neg, neg);

		// Total number of vertices in the box (not unique vertices)
		int totalVertices = sideCoords.length + bottomCoords.length;
		// This array specifies how many vertices are in each strip
		// We have 2 strips, the sides and the bottom
		int[] stripCounts = { sideCoords.length, bottomCoords.length };

		// Using a TriangleStripArray
		TriangleStripArray tsa = new TriangleStripArray(totalVertices,
				TriangleStripArray.COORDINATES, stripCounts);
		// Add the side coordinates to the strip array
		tsa.setCoordinates(0, sideCoords);
		// Add the bottom coordinates, after the side coordinates
		tsa.setCoordinates(sideCoords.length, bottomCoords);

		return tsa;
	}

	@Override
	public Appearance createAppearance() {
		// Let's make it light blue
		ColoringAttributes ca = new ColoringAttributes();
		ca.setColor(0.3f, 0.3f, 1.0f);

		PolygonAttributes pa = new PolygonAttributes();
		// This means all faces will be drawn
		pa.setCullFace(PolygonAttributes.CULL_NONE);
		// This means it will only draw the lines, so you can see the triangles
		// pa.setPolygonMode(PolygonAttributes.POLYGON_LINE);

		// Set the appearance
		Appearance app = new Appearance();
		app.setColoringAttributes(ca);
		app.setPolygonAttributes(pa);
		// other attributes we didn't change from defaults:
		// LineAttributes, PointAttributes, RenderingAttributes,
		// TextureAttributes, TransparencyAttributes

		return app;
	}
}
